local skel = fk.CreateSkill {
  name = "emo__kuyi",
}

Fk:loadTranslationTable{
  ["emo__kuyi"] = "苦肄",
  [":emo__kuyi"] = "你可以将两张牌当一张【杀】或【闪】使用或打出，结算后若这两张牌类型相同，你获得一张同类型牌，若类型不同，你获得一张类型与两者均不同的牌。你根据每回合首次因此技能获得的牌的类型于本轮获得技能：基本〖勤政〗；锦囊〖承启〗；装备〖营图〗。",

  ["#emo__kuyi"] = "苦肄：将两张牌当【杀】或【闪】使用或打出",
}

skel:addEffect("viewas", {
  anim_type = "drawcard",
  pattern = "slash,jink",
  prompt = "#emo__kuyi",
  mute_card = false,
  interaction = function(self, player)
    local names = player:getViewAsCardNames(self.name, {"slash", "jink"})
    if #names > 0 then
      return UI.CardNameBox { choices = names }
    end
  end,
  handly_pile = true,
  card_filter = function (self, player, to_select, selected)
    return #selected < 2
  end,
  view_as = function (self, player, cards)
    if #cards ~= 2 or Fk.all_card_types[self.interaction.data] == nil then return nil end
    local c = Fk:cloneCard(self.interaction.data)
    c.skillName = self.name
    c:addSubcards(cards)
    return c
  end,
  after_use = function (self, player, use)
    local subcards = use.card.subcards
    if #subcards < 2 then return end
    local room = player.room
    if player.dead then return end
    local types = {"basic", "trick", "equip"}
    for _, id in ipairs(subcards) do
      table.removeOne(types, Fk:getCardById(id):getTypeString())
    end
    local cardType = (#types == 1) and types[1] or Fk:getCardById(subcards[1]):getTypeString()
    local ids = room:getCardsFromPileByRule(".|.|.|.|.|"..cardType, 1, "allPiles")
    if #ids > 0 then
      room:moveCardTo(ids, Player.Hand, player, fk.ReasonJustMove, self.name, nil, true, player)
      if not player.dead and player:getMark("emo__kuyi_gain-turn") == 0 then
        room:setPlayerMark(player, "emo__kuyi_gain-turn", 1)
        local list = {["basic"] = "emo__qinzheng", ["trick"] = "emo__chengqi", ["equip"] = "emo__yingtu"}
        local skill = list[cardType]
        if Fk.skills[skill] and not player:hasSkill(skill, true) then
          room:addTableMark(player, "emo__kuyi_skill-round", skill)
          room:handleAddLoseSkills(player, skill)
        end
      end
    end
  end,
  enabled_at_play = function(self, player)
    return player:canUse(Fk:cloneCard("slash"))
  end,
  enabled_at_response = function (self, player, response)
    return true
  end,
})

skel:addEffect(fk.RoundEnd, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:getMark("emo__kuyi_skill-round") ~= 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    player.room:handleAddLoseSkills(player, "-"..table.concat(player:getTableMark("emo__kuyi_skill-round"), "|-"))
  end,
})

return skel
